﻿using QuarryPlanner.Domain.DTOs;
using QuarryPlanner.Domain.Interfaces.Persistence;
using QuarryPlanner.Persistence.Config;
using System;
using System.Collections.Generic;
using System.Linq;

namespace QuarryPlanner.Persistence.Repositories
{
    // Author: Ryan Redburn
    // Created: 10/20/2014

    #region [ Updates ]

    // Author: Ryan Redburn
    // Date: 11/5/2014
    // Revision: Added methods for interacting with MobileVehicleExamDTO. Removed pointless hardcoded context dependency.

    #endregion

    /// <summary>
    /// Repository class used to interact with the QRP data source for QuarryHours.
    /// </summary>
    public sealed class DailyReportRepository : IDailyReportRepository
    {
        #region [ Fields ]

        private readonly QRPContext _context;

        #endregion

        #region [ Constructors ]

        /// <summary>
        /// Creates an instance of DailyReportRepository.
        /// </summary>
        public DailyReportRepository()
        {
            _context = new QRPContext();
        }

        #endregion

        #region [ Methods ]

        #region [ QuarryHours ]

        /// <summary>
        /// Retrieves the QuarryHoursDTO with the given ID.
        /// </summary>
        /// <param name="id">QuarryHoursDTO ID.</param>
        /// <returns>QuarryHoursDTO.</returns>
        public QuarryHoursDTO FindQuarryHoursByID(int id)
        {
            if (id <= 0)
                throw new ArgumentOutOfRangeException("id", "FindQuarryHoursByID does not accept an integer less than one as an argument.");

            var qr = _context.QuarryHours.SingleOrDefault(q => q.QuarryHoursID == id);

            if (qr == null)
                throw new InvalidOperationException("No QuarryHours with the given ID was found.");

            return qr;
        }

        /// <summary>
        /// Retrieves all QuarryHoursDTOs.
        /// </summary>
        /// <returns>IEnumerable of QuarryHoursDTO.</returns>
        public IEnumerable<QuarryHoursDTO> FindAllQuarryHours()
        {
            return _context.QuarryHours;
        }

        /// <summary>
        /// Inserts the given QuarryHoursDTO.
        /// </summary>
        /// <param name="quarryHours">New QuarryHoursDTO to save.</param>
        public void Insert(QuarryHoursDTO quarryHours)
        {
            _context.QuarryHours.Add(quarryHours);
            _context.SaveChanges();
        }

        /// <summary>
        /// Deletes an existing QuarryHoursDTO.
        /// </summary>
        /// <param name="quarryHours">QuarryHoursDTO to delete.</param>
        public void Delete(QuarryHoursDTO quarryHours)
        {
            var qr = _context.QuarryHours.SingleOrDefault(x => x.QuarryHoursID == quarryHours.QuarryHoursID);

            if (qr == null)
                throw new InvalidOperationException("No QuarryHours with the given ID was found.");

            _context.QuarryHours.Remove(qr);
            _context.SaveChanges();
        }

        #endregion

        #region [ MVE ]

        /// <summary>
        /// Retrieves the MobileVehicleExamDTO with the given ID.
        /// </summary>
        /// <param name="id">MobileVehicleExamDTO ID.</param>
        /// <returns>MobileVehicleExamDTO.</returns>
        public MobileVehicleExamDTO FindMVEByID(int id)
        {
            if (id <= 0)
                throw new ArgumentOutOfRangeException("id", "FindMVEByID does not accept an integer less than one as an argument.");

            var mve = _context.MobileVehicleExams.SingleOrDefault(x => x.MobileVehicleExamID == id);

            if (mve == null)
                throw new InvalidOperationException("No MobileVehicleExam with the given ID was found.");

            return mve;
        }

        /// <summary>
        /// Retrieves all MobileVehicleExamDTOs.
        /// </summary>
        /// <returns>IEnumerable of MobileVehicleExamDTO.</returns>
        public IEnumerable<MobileVehicleExamDTO> FindAllMVE()
        {
            return _context.MobileVehicleExams;
        }

        /// <summary>
        /// Inserts the given MobileVehicleExamDTO.
        /// </summary>
        /// <param name="mve">New MobileVehicleExamDTO to save.</param>
        public void Insert(MobileVehicleExamDTO mve)
        {
            _context.MobileVehicleExams.Add(mve);
            _context.SaveChanges();
        }

        /// <summary>
        /// Deletes an existing MobileVehicleExamDTO.
        /// </summary>
        /// <param name="mve">MobileVehicleExamDTO to delete.</param>
        public void Delete(MobileVehicleExamDTO mve)
        {
            var delete = _context.MobileVehicleExams.SingleOrDefault(x => x.MobileVehicleExamID == mve.MobileVehicleExamID);

            if (mve == null)
                throw new InvalidOperationException("No MobileVehicleExam with the given ID was found.");

            _context.MobileVehicleExams.Remove(mve);
            _context.SaveChanges();
        }

        #endregion

        #region [ WPE ]

        /// <summary>
        /// Retrieves the WorkPlaceExamDTO with the given ID.
        /// </summary>
        /// <param name="id">WorkPlaceExamDTO ID.</param>
        /// <returns>WorkPlaceExamDTO.</returns>
        public WorkPlaceExamDTO FindWPEByID(int id)
        {
            if (id <= 0)
                throw new ArgumentOutOfRangeException("id", "FindWPEByID does not accept an integer less than one as an argument.");

            var wpe = _context.WorkPlaceExams.SingleOrDefault(x => x.WorkPlaceExamID == id);

            if (wpe == null)
                throw new InvalidOperationException("No WorkPlaceExam with the given ID was found.");

            return wpe;
        }

        /// <summary>
        /// Retrieves all WorkPlaceExamDTO.
        /// </summary>
        /// <returns>IEnumerable of WorkPlaceExamDTO.</returns>
        public IEnumerable<WorkPlaceExamDTO> FindAllWPE()
        {
            return _context.WorkPlaceExams;
        }

        /// <summary>
        /// Inserts the given WorkPlaceExamDTO.
        /// </summary>
        /// <param name="wpe">New WorkPlaceExamDTO to save.</param>
        public void Insert(WorkPlaceExamDTO wpe)
        {
            _context.WorkPlaceExams.Add(wpe);
            _context.SaveChanges();
        }

        /// <summary>
        /// Deletes an existing WorkPlaceExamDTO.
        /// </summary>
        /// <param name="wpe">WorkPlaceExamDTO to delete.</param>
        public void Delete(WorkPlaceExamDTO wpe)
        {
            var delete = _context.WorkPlaceExams.SingleOrDefault(x => x.WorkPlaceExamID == wpe.WorkPlaceExamID);

            if (delete == null)
                throw new InvalidOperationException("No WorkPlaceExam with the given ID was found.");

            _context.WorkPlaceExams.Remove(delete);
            _context.SaveChanges();
        }

        #endregion

        #endregion
    }
}
